package com.gmail.ceac13;

import java.util.List;

import com.gmail.ceac13.servicos.ImageServer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RequisicoesAdapter extends BaseAdapter {
	List <Friend> lista;
	private Context context;
	int user_id;
	Friend friend;
	//List<Friend> list;
	SharedPreferences settings;
	String endServer;
	
	public RequisicoesAdapter(Context context, List<Friend> lista, int id) {
		this.context = context;
		this.lista = lista;
		user_id = id;
		settings = context.getSharedPreferences("ConnectMe",context.MODE_PRIVATE);
		endServer = settings.getString("servidor", "http://connectme.scienceontheweb.net/"); 
		//endServer = settings.getString("servidor", "http://10.0.2.2/connectme/");
	}
	
	@Override
	public int getCount() {
		return this.lista.size();
	}

	@Override
	public Object getItem(int arg0) {
		return this.lista.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return this.lista.get(arg0).getId();
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		/*Friend*/ friend = lista.get(arg0);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.requisicao, null);
		ImageView foto = (ImageView) v.findViewById(R.id.foto_requisicao);
		TextView nome = (TextView) v.findViewById(R.id.nome_requisicao);
		/*
		if (friend.getFoto() == -1)
			foto.setImageResource(R.drawable.usuariopadrao);
		else
			foto.setImageResource(friend.getFoto());
		*/
		if (friend.getFoto().equals("-1"))
			foto.setImageResource(R.drawable.usuariopadrao);
		else
			foto.setImageDrawable(ImageServer.buscarImagem(friend.getFoto(), context));
		nome.setText(friend.getNome());
		Button aceitar = (Button) v.findViewById(R.id.requisicao_aceitar);
		Button negar = (Button) v.findViewById(R.id.requisicao_negar);
		
		aceitar.setOnClickListener(new OnClickListener() {
			int friend_id = friend.getId();
			
	    	
			@Override
			public void onClick(View v) {
				if (Servidor.responderRequisicao(user_id, friend_id, true, endServer)) {
					carregarLista();
					notifyDataSetChanged();
					Toast t = Toast.makeText(context, "Requisição aceita com sucesso.", Toast.LENGTH_SHORT);
					t.show();
					
				}
				else {
				}
			}
		});
		negar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Servidor.responderRequisicao(user_id, friend.getId(), false, endServer)) {
					carregarLista();
					notifyDataSetChanged();
					Toast t = Toast.makeText(context, "Requisição removida.", Toast.LENGTH_SHORT);
					t.show();
				}
			}
		});
		
		return v;
	}

	public void carregarLista() {
		//settings = context.getSharedPreferences("ConnectMe",context.MODE_PRIVATE);
        int user_id = settings.getInt("user_id", -1);
		lista = Servidor.listarRequisicoes(user_id, endServer);
	}
	
}
